package com.zhaozhe.sort;

/**
 * Created by ghjou on 2018/3/7.
 */
public class BinaryFind {

    public static int binaryFind(long[] data,long target){
        int start = 0;
        int end = data.length - 1;
        while (start < end){
            int middleIndex = (start + end) / 2;
            if (target == data[middleIndex]){
                return middleIndex;
            }
            if (target >= data[middleIndex]){
                start = middleIndex + 1;
            } else {
                end = middleIndex - 1;
            }
        }
        return -1;
    }

    public static void binaryFindTest(long[] data, long tartget){
        long start = System.nanoTime();
        int result = binaryFind(data, tartget);
        long end = System.nanoTime();
        System.out.println("binary search position: " + result);
        System.out.println("binary search time: " + (end - start));
    }
}
